package com.boarsoft.boar.soagov.face.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.entity.FaceParam;
import com.boarsoft.boar.soagov.face.FaceParamBiz;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.action.PageAction;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;

@SuppressWarnings("serial")
@Component
@Scope("prototype")
@ParentPackage("default-package")
@Namespace(value = "/face/param")
public class FaceParamAction extends PageAction {
	@Autowired
	private FaceParamBiz faceParamBiz;
	//
	private String id;
	private String faceId;
	private String code;
	private String name;
	private String type;
	private int len;
	private short flag; // 0:入参，1：出参
	private String memo;

	/**
	 * 按接口ID查询所有的参数<br/>
	 */
	@Action("list")
	@Authorized(code = "face.param.list")
	public void list() {
		if (Util.strIsEmpty(faceId)) {
			// String[] faceIds = id.split(",");
			// id = 'xxxxx','xxxxx'
			List<FaceParam> lt = faceParamBiz.listOutputOf(id);
			writeReply(true, JsonUtil.from(lt));
		} else {
			PagedResult<FaceParam> pr = faceParamBiz.list(faceId, flag, pageNo, pageSize);
			writeReply(true, JsonUtil.from(pr));
		}
	}

	@Action("save")
	@Authorized(code = "face.param.save")
	public void save() {
		ReplyInfo<Object> ro = new ReplyInfo<Object>();
		// if (!this.isValid())
		// return;
		//
		if (Util.strIsEmpty(id)) {
			FaceParam c = new FaceParam();
			c.setFaceId(faceId);
			c.setCode(code);
			c.setName(name);
			c.setType(type);
			c.setLen(len);
			c.setFlag(flag);
			c.setMemo(memo);
			ro = faceParamBiz.add(c);
		} else {
			FaceParam c = faceParamBiz.get(id);
			c.setName(name);
			c.setType(type);
			c.setLen(len);
			c.setMemo(memo);
			ro = faceParamBiz.update(c);
		}
		if (ro.isSuccess()) {
			writeReply();
		} else {
			writeReply(String.valueOf(ro.getData()));
		}
	}

	@Action("delete")
	@Authorized(code = "face.param.delete")
	public void delete() {
		if (!this.isValid("id")){
			writeReply(Constants.ERROR_INVALID,"Msg.common.param");
			return;
		}
		faceParamBiz.delete(id);
		writeReply();
	}

	@Action("get")
	@Authorized(code = "face.param.get")
	public void get() {
		if (!this.isValid("id")){
			writeReply(Constants.ERROR_INVALID,"Msg.common.param");
			return;
		}
		FaceParam a = faceParamBiz.get(id);
		writeReply(true, JsonUtil.from(a));
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFaceId() {
		return faceId;
	}

	public void setFaceId(String faceId) {
		this.faceId = faceId;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getLen() {
		return len;
	}

	public void setLen(int len) {
		this.len = len;
	}

	public short getFlag() {
		return flag;
	}

	public void setFlag(short flag) {
		this.flag = flag;
	}

	public void setFaceParamBiz(FaceParamBiz faceParamBiz) {
		this.faceParamBiz = faceParamBiz;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

}